<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择出生日期"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="onCancel"
  />
</template>

<script>
import { updateUserInfo } from "@/api/User";
import dayjs from "dayjs";
export default {
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.value),
    };
  },
  methods: {
    async onConfirm(value) {
      // value就是选中的日期
      const birthday = dayjs(value).format("YYYY-MM-DD");
      try {
        await updateUserInfo({
          birthday: birthday,
        });
        this.$emit("input", birthday);
        this.$emit("close");
      } catch (error) {
        console.log(error);
        this.$toast.fail("更新失败");
      }
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style></style>
